home *** CD-ROM | disk | FTP | other *** search
- real dataonly[][2];
- real xdata[],ydata[];
- real dataMax, dataMin; ** corrected for roundoff error
- integer maxSize; ** maximum size of data table (see createBlock msg)
- integer max; ** length of data array
- integer x,y;
- integer xPoint; ** for incrementing x index on CurrentTime input
- real tempx,tempy,xvalue,yvalue;
- double timeArray[];
-
- ** This block generates a curve of data over time.
- ** Copyright © 1989-1994 by Imagine That, Inc.
- ** All Rights Reserved.
- ** Extend Generic Library, Input Data block:Alfy Riddle 4/10/89
- ** modified 1/1/92 JSL modified for V2.0
- ** 8/27/92 JSL removed on yOut
- ** 10/6/93 JSL changed default to stepped
- ** 2/14/94 DJK modified trace and report
- ** 3/10/94 DJK formatted report
- ** 3/10/94 DJK added block label to report & trace
- **
- ** The Sort function of this block came from the:
- ** Nonlinear Function Generator 2/20/89
- ** Authors: Cheryl Blanford and Chuck Oman
- ** MIT Man Vehicle Laboratory
- ** Rm 37-219, Cambridge, MA 02139
-
- procedure calc()
- {
- integer lastX, notDone;
- lastX = 0; ** this is a starting point for the interpolation
- ** it keeps track of the last interpolated point
-
- **
- ** dataMax & dataMin are calculated on initSim
- ** read in x value and interpolate to find the y value.
- ** 'max' gives the number of data points & is set during initSim
- **
-
- if( repeat )
- {
- if( currentTime >= repTime )
- xPoint = -1; ** reset this value
-
- xValue = realMod(currentTime,repTime);
- }
- else
- xValue = currentTime;
-
- ** get current valid range of data (xPoint = -1 in initSim)
- while( (xValue*1.00000000001 >= data[xPoint+1][0]) && (xPoint+1 < max) )
- {
- xPoint++;
- }
-
- if( xPoint+1 >= max ) ** data at last point or too high
- {
- if( (xPoint == max-1) && (realAbs(xValue-data[xPoint][0]) < 1.0e-15) )
- yOut = data[xPoint][1];
- else ** ERROR - above range
- {
- if( stop )
- {
- userError
- ("Simulation time value has exceeded highest time value in block number "+(MyBlockNumber()));
- abort;
- }
- else
- yOut = 0.0;
- }
- }
- else ** data in or below range
- {
- if( (xPoint >= 0) && (xPoint < max-1) )
- {
- if( interp ) ** interpolate
- {
- yValue = ((xValue - data[xPoint][0])*(data[xPoint+1][1] -
- data[xPoint][1])/(data[xPoint+1][0] - data[xPoint][0]))
- + data[xPoint][1];
- }
- else ** stepped
- {
- yValue = data[xPoint][1];
- }
-
- yOut = yValue;
- }
- else ** data near first point or ERROR
- {
- if( (realAbs(xValue-data[0][0]) < 1.0e-15) )
- yOut = data[0][1];
- else ** ERROR - below range
- {
- if( stop )
- {
- userError
- ("Simulation time value is below lowest time value in block number "+(MyBlockNumber()));
- abort;
- }
- else
- yOut = 0.0;
- }
- }
- }
-
- ** sysGlobal2 is the file reference number for the DEBUG TRACE
- if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal2,"Input Data block number "+(MyBlockNumber())+". Current Time:"+currentTime+".","",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- fileWrite(sysGlobal2," Time = "+currentTime,"",True);
- fileWrite(sysGlobal2," Output = "+yOut,"",True);
- fileWrite(sysGlobal2," ","",True);
- }
- }
-
-
- Procedure validMax()
- {
- integer i;
- i=0;
-
- while( !noValue(data[i][0]) && i<maxSize )
- i++;
- max = i;
-
- if (max <= 1) ** check for at least 2 rows
- {
- usererror("Must have at least two rows of data in Input Data block number "+(MyBlockNumber()));
- abort;
- }
- }
-
-
- Procedure sortFcn()
- {
- validMax();
-
- ** split data into 2 arrays for ease of sorting.
- makearray(dataonly,max);
- makearray(xdata,max);
- makearray(ydata,max);
-
- for (x=0;x<max;x++)
- {
- dataonly[x][0] = data[x][0];
- dataonly[x][1] = data[x][1];
- }
-
- for(x = 0 ;x<max;x++)
- {
- xdata[x] = data[x][0];
- ydata[x] = data[x][1];
- }
-
- for (x=0;x<max;x++)
- for (y=x+1;y<max;y++)
- if(xdata[y] < xdata[x])
- {
- tempx = xdata[y];
- tempy = ydata[y];
- xdata[y] = xdata[x];
- ydata[y] = ydata[x];
- xdata[x] = tempx;
- ydata[x] = tempy;
- }
-
-
- **restore table, now properly sorted.
- for (x = 0;x<max;x++ )
- {
- data[x][0] = xdata[x];
- data[x][1] = ydata[x];
- }
- }
-
-
- on choosetoplot
- {
- integer k, numPlot;
- validMax();
-
- ** support stepped plots
- if( interp )
- numPlot = max;
- else if( stepped )
- numPlot = 2 * max - 1;
-
- makearray(xdata,numPlot);
- makearray(ydata,numPlot);
-
- for (k=0;k<max;k++)
- {
- if( interp )
- {
- xdata[k] = data[k][0];
- ydata[k] = data[k][1];
- }
- else ** stepped
- {
- xdata[2*k] = data[k][0];
- ydata[2*k] = data[k][1];
-
- if( k < max-1 ) ** odd number of points (skip last)
- {
- xdata[2*k+1] = data[k+1][0]; ** x coordinates
- ydata[2*k+1] = data[k][1];
- }
- }
- }
-
- ** install the axes
- installAxis(0, "Input Data",
- "Time", FALSE, 0,20,
- "Y Output", FALSE, 0,20, "", 0, 0, 0,
- blackpattern, blackcolor, 100);
- installArray(0, 0, "Time", xdata, 0, 20,
- 0, 0, blackPattern, cyanColor);
- installArray(0, 1, "Y Output", ydata, 0, 20,
- 0, 0, dkgrayPattern, redColor);
-
- makeScatter(0, 0);
- ** plot the data
-
- for (k=0;k<numPlot;k++)
- plotNewScatter(0,0,k,xdata[k],ydata[k]);
-
- autoscalex(0);
- autoscaley(0);
- showPlot(0, "Input Data");
- }
-
-
-
- ** This message occurs for each step in the simulation.
- on simulate
- {
- calc();
- }
-
-
- on endsim
- {
- integer i;
-
- ** sysGlobal1 is the file reference number for the TEXT REPORT
- if( sysGlobal1 != 0.0 ) ** 0 is error, check for open file for REPORT
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal1,"Input Data block number "+MyBlockNumber(),"",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- if( interp )
- fileWrite(sysGlobal1," Interpolated","",True);
- else
- fileWrite(sysGlobal1," Stepped","",True);
-
- if( stop )
- fileWrite(sysGlobal1," Stops when out of range","",True);
- else
- fileWrite(sysGlobal1," Sets output to zero when out of range","",True);
-
- if( repeat )
- fileWrite(sysGlobal1," Repeats every "+repTime+" time units","",True);
-
- fileWrite(sysGlobal1," Table time y","",TRUE);
-
- validMax();
-
- for( i=0;i<max;i++ ) ** picks up old itemNum
- fileWrite(sysGlobal1," "+RealToStr(data[i][0],8)+" "+RealToStr(data[i][1],8),"",TRUE);
-
- // for( i=0;i<max;i++ ) ** picks up old itemNum
- // fileWrite(sysGlobal1," "+data[i][0]+", "+data[i][1],"",TRUE);
-
- fileWrite(sysGlobal1," ","",True);
- }
- }
-
- on createBlock
- {
- maxSize = 500;
- interp = 0;
- stepped = 1;
- stop = 1;
- zero = 0;
- repeat = 0;
- repTime = 10;
- }
-
- on checkdata
- {
- sysGlobal1 = 0.0; ** prevent false reports
- sysGlobal2 = 0.0; ** prevent false debugs
-
- validMax(); ** check for at least 2 rows
- }
-
-
- ** Initialize any simulation variables.
- on initsim
- {
- sortFcn(); ** sort
-
- if( data[max-1][0] > 0 ) ** for roundoff error
- dataMax = data[max-1][0]*1.000000001;
- else
- dataMax = data[max-1][0]*0.999999999;
-
- if( data[0][0] > 0 )
- dataMin = data[0][0]*0.999999999;
- else
- dataMin = data[0][0]*1.000000001;
-
- xPoint = -1; ** for currentTime mode
-
- if( repeat )
- zero = 1; ** do not abort when data out of range
-
- if( getPassedArray(sysGlobal0, timeArray) )
- getSimulateMsgs(FALSE);
- }
-
-
- on sort
- {
- sortFcn(); ** then sort it
- }